﻿using System;
using System.Web.Services;
using QuanLyHopDong.Biz;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class VatTuTheoTuyen : System.Web.UI.Page
    {
        private KhoiLuongBiz _khoiLuongBiz;

        private KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        private int _khoiLuongId;

        protected int KhoiLuongId
        {
            get
            {
                if (_khoiLuongId == 0)
                {
                    int.TryParse(Request.Params["khoiLuongId"], out _khoiLuongId);
                }
                return _khoiLuongId;
            }
        }

        private static KhoiLuongVatTuBiz _khoiLuongVatTuBiz;
        private static KhoiLuongVatTuBiz KhoiLuongVatTuManager
        {
            get
            {
                return _khoiLuongVatTuBiz ?? (_khoiLuongVatTuBiz = new KhoiLuongVatTuBiz());
            }
        }

        #region Events
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (KhoiLuongId == 0)
            {
                return;
            }

            if (!IsPostBack)
            {
                LoadKhoiLuongInfo();
            }
        }

        #endregion

        #region Helpers

        private void LoadKhoiLuongInfo()
        {
            var khoiLuong = KhoiLuongManager.GetById(KhoiLuongId);
            if (khoiLuong != null)
            {
                ltrMaTuyen.Text = khoiLuong.MaTuyen;
                ltrDiemDauCuoi.Text = string.Format("{0} - {1}", khoiLuong.DiemDau, khoiLuong.DiemCuoi);
                ltrChieuDaiTuyen.Text = string.Format("{0:0}", khoiLuong.ChieuDaiTuyen);
                ltrDoiThiCong.Text = khoiLuong.TenDoiThiCong;
            }
        }

        #endregion

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object GetKhoiLuongVatTus(int khoiLuongId)
        {
            return new { Result = "OK", Records = KhoiLuongVatTuManager.KhoiLuongVatTuTheoTuyen(khoiLuongId) };
        }

        #endregion
    }
}